package com.wsc.wisdomchinaqinghe.activitity.minemodule;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseFragmentActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.fragment.YuYueOneFragment;
import com.wsc.wisdomchinaqinghe.fragment.YuYueThreeFragment;
import com.wsc.wisdomchinaqinghe.fragment.YuYueTwoFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Description 新股列表页
 * @author jaily.zhang
 * @date 2015-12-28 下午2:08:43
 * @version V1.3.1
 */
public class MyYuYueActivity extends BaseFragmentActivity implements OnClickListener {

	private ViewPager fragment_bygz_viewPager;//放置Fragment

	private List<Fragment> bygzFragments;//对应页面集合
	private TextView tv_1;
	private TextView tv_2;
	private TextView tv_3;
	private ImageView regist_back;
	private TextView tv_headtitle;
	private String name;
	private boolean isPause;
	private MyApplication myApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bygz_layout);
		initView();
		Intent intent=getIntent();
		name=intent.getStringExtra("name");
		tv_headtitle.setText(name);
		myApplication=(MyApplication) getApplication();

		bygzFragments = new ArrayList<>();
		YuYueOneFragment guoFJY_fragment = new YuYueOneFragment();//全部
		YuYueTwoFragment bingYGZ_fragment = new YuYueTwoFragment();//进行中
		YuYueThreeFragment bingYDJ_fragment = new YuYueThreeFragment();//待评价

		bygzFragments.add(guoFJY_fragment);
		bygzFragments.add(bingYGZ_fragment);
		bygzFragments.add(bingYDJ_fragment);
		myApplication.setSelectShouliCenterIndex(0);

		fragment_bygz_viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), bygzFragments));//
		fragment_bygz_viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	private void initView() {
		fragment_bygz_viewPager=(ViewPager) findViewById(R.id.fragment_bygz_viewPager);//放置Fragment
		tv_1=(TextView) findViewById(R.id.tv_1);
		tv_2=(TextView) findViewById(R.id.tv_2);
		tv_3=(TextView) findViewById(R.id.tv_3);
		tv_1.setOnClickListener(this);
		tv_2.setOnClickListener(this);
		tv_3.setOnClickListener(this);
		regist_back=(ImageView) findViewById(R.id.regist_back);
		tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
		regist_back.setOnClickListener(this);

	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.regist_back:
				finish();
				break;
			case R.id.tv_1:
				tv_1.setTextColor(getResources().getColor(R.color.D7F2));
				tv_2.setTextColor(getResources().getColor(R.color.text_color_one));
				tv_3.setTextColor(getResources().getColor(R.color.text_color_one));
				fragment_bygz_viewPager.setCurrentItem(0);
				break;

			case R.id.tv_2:
				tv_1.setTextColor(getResources().getColor(R.color.text_color_one));
				tv_2.setTextColor(getResources().getColor(R.color.D7F2));
				tv_3.setTextColor(getResources().getColor(R.color.text_color_one));
				fragment_bygz_viewPager.setCurrentItem(1);
				break;

			case R.id.tv_3:
				tv_1.setTextColor(getResources().getColor(R.color.text_color_one));
				tv_2.setTextColor(getResources().getColor(R.color.text_color_one));
				tv_3.setTextColor(getResources().getColor(R.color.D7F2));
				fragment_bygz_viewPager.setCurrentItem(2);
				break;
		}
	}

	class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int current) {
			// TODO Auto-generated method stub
			// viewPager.setCurrentItem(current);
			myApplication.setSelectShouliCenterIndex(current);
			for(int i=0;i<bygzFragments.size();i++)
			{
				if(i==current)
				{
					bygzFragments.get(i).onResume();
				}else {
					bygzFragments.get(i).onPause();
				}
			}
			if(current==0)
			{
				tv_1.setTextColor(getResources().getColor(R.color.little_white));
				tv_2.setTextColor(getResources().getColor(R.color.black));
				tv_3.setTextColor(getResources().getColor(R.color.black));
			}else if(current==1)
			{
				tv_1.setTextColor(getResources().getColor(R.color.black));
				tv_2.setTextColor(getResources().getColor(R.color.little_white));
				tv_3.setTextColor(getResources().getColor(R.color.black));
			}else
			{
				tv_1.setTextColor(getResources().getColor(R.color.black));
				tv_2.setTextColor(getResources().getColor(R.color.black));
				tv_3.setTextColor(getResources().getColor(R.color.little_white));
			}
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		isPause=true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(isPause)
		{
			for(int i=0;i<bygzFragments.size();i++)
			{
				if(i==myApplication.getSelectShouliCenterIndex())
				{
					bygzFragments.get(i).onResume();
				}else {
					bygzFragments.get(i).onPause();
				}
			}
		}
		isPause=false;

	}

	class ViewPagerAdapter extends FragmentStatePagerAdapter {

		private List<Fragment> listfragmentView;// viewList 集合

		public ViewPagerAdapter(FragmentManager fm, List<Fragment> listfragmentView) {
			super(fm);
			this.listfragmentView = listfragmentView;
			// TODO Auto-generated constructor stub
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			return listfragmentView.get(position);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listfragmentView.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub

			Fragment ff = (Fragment) super.instantiateItem(container, position);
			ff.setTargetFragment(listfragmentView.get(position), position);

			return ff;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			// TODO Auto-generated method stub
			super.destroyItem(container, position, object);
		}

	}
}
